package com.glzn.springboot03.controller;

import com.github.pagehelper.PageInfo;
import com.glzn.springboot03.entity.Para;
import com.glzn.springboot03.service.ParaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gongjq
 */
@Controller
public class ParaController  {
	private static final Logger logger = LoggerFactory.getLogger(ParaController.class);

	private static final String ALL_TYPE = "所有类型";

	@Autowired
	private ParaService paraService;

	private List<String> paraType = new ArrayList<>();

	private ArrayList<Integer> areaNum = new ArrayList<>();

	@RequestMapping("/getpara")
	@ResponseBody
	public String getPara() throws UnsupportedEncodingException {
		return paraService.listPara();
	}

	@GetMapping(value = "/getpara/getprocess")
	@ResponseBody
	public int getProcess(HttpSession session){
		return paraService.getProcess(session.getId());
	}

	@RequestMapping("/paras")
	public String para(ModelMap map,
	                 HttpSession session,
	                 @RequestParam(required = false, defaultValue = "0")int areaNum,
	                 @RequestParam(required = false)String type,
	                 @RequestParam(required = false, defaultValue = "1") int pageNum,
	                 @RequestParam(required = false, defaultValue = "10") int pageSize){
		Integer deviceId = (Integer) session.getAttribute("device_id");
		//获取通道号
		if(this.areaNum.size() == 0){
			List<Para> all = paraService.findAll(deviceId);
			for (Para para : all) {
				int j = 0;
				for (j = 0; j < this.areaNum.size(); j++) {
					if (this.areaNum.get(j).equals(para.getAreaNum())){
						break;
					}
				}
				if (j == this.areaNum.size()){
					this.areaNum.add(para.getAreaNum());
				}
			}
		}
		if (type == null || "".equals(type)) {
			type = ALL_TYPE;
		}
		//添加类型
		this.paraType.clear();
		this.paraType.add(ALL_TYPE);
		List<Para> paraList = paraService.findByAreaNum(areaNum, deviceId);
		for (Para para : paraList) {
			int i = 0;
			for (i = 0; i < this.paraType.size(); i++) {
				if (this.paraType.get(i).equals(para.getType())){
					break;
				}
			}
			if (i == this.paraType.size()){
				this.paraType.add(para.getType());
			}
		}
		PageInfo<Para> pageInfo;
		if (ALL_TYPE.equals(type)){
			pageInfo = paraService.findByAreaNum(areaNum, pageNum, pageSize, deviceId);
		}else {
			pageInfo = paraService.findByTypeAndAreaNum(type, areaNum, pageNum, pageSize, deviceId);
		}
		List<Para> pageInfoList = pageInfo.getList();
		List<String> valueList = new ArrayList<>();
		List<String> rangeList = new ArrayList<>();
		for (Para para : pageInfoList) {
			String option = para.getOption();
			if (option==null){
				//数值型值 value/10^decBitNum
				double tmp = para.getValue()/Math.pow(10, para.getDecBitNum());
				DecimalFormat df = getFormatByNum(para.getDecBitNum());
				String s1 = MessageFormat.format("{0}{1}~{2}{1}",df.format(para.getMinValue()/Math.pow(10, para.getDecBitNum())), para.getUnit(), df.format(para.getMaxValue()/Math.pow(10, para.getDecBitNum())));
				valueList.add(df.format(tmp)+para.getUnit());
				rangeList.add(s1);
			}else {
				Integer value = para.getValue();
				int startIndex = findIndex(option, '、', value);
				int endIndex = findIndex(option, '、', value+1);
				if (startIndex > 0){
					startIndex += 1;
				}
				String s = option.substring(startIndex, endIndex);
				//范围
				String s1 = option.substring(0,option.indexOf("、命名列表越限"));
				valueList.add(s);
				rangeList.add(s1);
			}
		}
		long totalNum = pageInfo.getTotal() / pageSize + 1;

		map.put("pageInfo",pageInfo);
		map.put("totalNum", totalNum);
		map.put("paraType", this.paraType);
		map.put("valueList", valueList);
		map.put("rangeList", rangeList);
		map.put("areaNum", this.areaNum);
		return "paras";
	}

	private DecimalFormat getFormatByNum(int num){
		DecimalFormat df;
		switch (num){
			case 1:
				df=new DecimalFormat("0.0");
				break;
			case 2:
				df=new DecimalFormat("0.00");
				break;
			case 3:
				df=new DecimalFormat("0.000");
				break;
			case 4:
				df=new DecimalFormat("0.0000");
				break;
			default:
				df=new DecimalFormat("0");
				break;
		}
		return df;
	}

	private int findIndex(String str,char cha, int num){
		int x=0;
		for(int i=0;i<num;i++){
			x=str.indexOf(cha,x+1);
		}
		return x;
	}

	@GetMapping("/wx/para")
	@ResponseBody
	public List<Para> wxPara(HttpSession session, int areaNum, String type){
		Integer deviceId = (Integer) session.getAttribute("device_id");
		if(ALL_TYPE.equals(type) || "".equals(type)){
			return paraService.findByAreaNum(areaNum, deviceId);
		}else {
			return paraService.findByTypeAndAreaNum(type, areaNum, deviceId);
		}
	}
}
